//
//  SelfPurchaseVehicleVC.m
//  MRBang
//
//  Created by 铅笔 on 16/5/7.
//  Copyright © 2016年 杭州易网新科技. All rights reserved.
//

#import "SelfPurchaseVehicleVC.h"

@interface SelfPurchaseVehicleVC ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property (nonatomic,strong) UITableView *tableView_addVehicle;
@property (nonatomic,strong) NSMutableArray *mutableArray_inputContent;

@end

@implementation SelfPurchaseVehicleVC

/**
 *数据懒加载
 */
- (NSMutableArray *)mutableArray_inputContent
{
    if (_mutableArray_inputContent==nil) {
        _mutableArray_inputContent = [[NSMutableArray alloc] init];
    }
    return _mutableArray_inputContent;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = COLOR_EVERYVIEWBG;
    self.title = @"自购车辆";
    
    self.mutableArray_inputContent = [@[@"请输入车辆品牌",@"请输入购买价格",@"请输入车牌号码",@"请输入登记日期"] mutableCopy];

    
    //添加 视图界面
    [self initWithSelfPurchaseVehicleVCSubviews];
}

#pragma mark --- 创建视图界面
- (void) initWithSelfPurchaseVehicleVCSubviews
{
    self.tableView_addVehicle = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 250)];
    [self.view addSubview:self.tableView_addVehicle];
    self.tableView_addVehicle.dataSource = self;
    self.tableView_addVehicle.delegate = self;
    self.tableView_addVehicle.showsVerticalScrollIndicator = NO;
    self.tableView_addVehicle.separatorStyle =UITableViewCellSeparatorStyleNone;
    self.tableView_addVehicle.backgroundColor = COLOR_EVERYVIEWBG;
    self.tableView_addVehicle.scrollEnabled = NO;
    
    //下一步 button
    UIButton *button_sureAction = [[UIButton alloc] initWithFrame:CGRectMake(48*PROPORTION_WIDTH,self.tableView_addVehicle.bottom+20,kScreenWidth - 48*PROPORTION_WIDTH*2, 93*PROPORTION_WIDTH)];
    [self.view addSubview:button_sureAction];
    [button_sureAction setTitle:@"确定" forState:UIControlStateNormal];
    [button_sureAction setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button_sureAction.layer.cornerRadius = 3.0;
    button_sureAction.clipsToBounds = YES;
    button_sureAction.backgroundColor = COLOR_BUTTON;
    [button_sureAction addTarget:self action:@selector(doSureAction) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark ---- UITableViewDataSource
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *cellVehicleID = @"CellVehicleID";
    UITableViewCell *cell_vehicle = [tableView cellForRowAtIndexPath:indexPath];
    if (cell_vehicle==nil) {
        cell_vehicle = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellVehicleID];
    }
    cell_vehicle.selectionStyle = UITableViewCellSelectionStyleNone;
    //横线
    UIView *view_line = [[UIView alloc] initWithFrame:CGRectMake(27*PROPORTION_WIDTH, cell_vehicle.height-1, kScreenWidth, 1.0)];
    [ cell_vehicle.contentView addSubview:view_line];
    view_line.backgroundColor = [BaseViewController colorWithHexString:@"#EEEFF1"];
    
    NSArray *array_title = @[@"车辆品牌",@"购买价格",@"车牌号码",@"车辆登记日期"];
    
    //UIlabel
    UILabel *label_leftTitle = [[UILabel alloc] initWithFrame:CGRectMake(27*PROPORTION_WIDTH, 27*PROPORTION_WIDTH, 210*PROPORTION_WIDTH, 20)];
    [cell_vehicle addSubview:label_leftTitle];
    label_leftTitle.text = array_title[indexPath.row];
    label_leftTitle.font = [UIFont systemFontOfSize:30*PROPORTION_WIDTH];
    label_leftTitle.textColor = [BaseViewController colorWithHexString:@"#666666"];
    
    //UITextField
    UITextField *textField_finll = [[UITextField alloc] initWithFrame:CGRectMake(label_leftTitle.right, label_leftTitle.y, 400*PROPORTION_WIDTH, label_leftTitle.height)];
    [cell_vehicle.contentView addSubview:textField_finll];
    textField_finll.font = [UIFont systemFontOfSize:30*PROPORTION_WIDTH];
    textField_finll.textColor = [BaseViewController colorWithHexString:@"#AAAAAA"];
    textField_finll.tag = indexPath.row+10;
    textField_finll.delegate = self;
    
    textField_finll.placeholder = self.mutableArray_inputContent[indexPath.row];
    
    if (indexPath.row==0) {
        if (self.dic_pathPersonInfo[@"carpp"]) {
            textField_finll.text = self.dic_pathPersonInfo[@"carpp"];
            
            [self.mutableArray_inputContent replaceObjectAtIndex:0 withObject:self.dic_pathPersonInfo[@"carpp"]];
        }
    }
    if (indexPath.row==1) {
        if (self.dic_pathPersonInfo[@"carprice"]) {
            textField_finll.text = self.dic_pathPersonInfo[@"carprice"];
            
             [self.mutableArray_inputContent replaceObjectAtIndex:0 withObject:self.dic_pathPersonInfo[@"carprice"]];
        }
    }

    if (indexPath.row==2) {
        if (self.dic_pathPersonInfo[@"carnum"]) {
            textField_finll.text = self.dic_pathPersonInfo[@"carnum"];
            
            [self.mutableArray_inputContent replaceObjectAtIndex:0 withObject:self.dic_pathPersonInfo[@"carnum"]];
        }
    }

    if (indexPath.row==3) {
        if (self.dic_pathPersonInfo[@"cartime"]) {
            textField_finll.text = self.dic_pathPersonInfo[@"cartime"];
            [self.mutableArray_inputContent replaceObjectAtIndex:0 withObject:self.dic_pathPersonInfo[@"cartime"]];
        }
    }
    return cell_vehicle;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //键盘退出
    [self commentTableViewTouchInSide];

}

//替换数组中的字段
- (void) textFieldDidEndEditing:(UITextField *)textField
{
   [self.mutableArray_inputContent replaceObjectAtIndex:textField.tag-10 withObject:textField.text];
    NSLog(@"mutableArray_inputContent  %@",self.mutableArray_inputContent);
}

#pragma mark  --- 点击tableView 收回键盘
- (void)commentTableViewTouchInSide
{
    UITextField *textField_temp0 = (UITextField *)[self.view viewWithTag:10];
    
    [textField_temp0 resignFirstResponder];
    
    UITextField *textField_temp1 = (UITextField *)[self.view viewWithTag:11];
    
    [textField_temp1 resignFirstResponder];
    
    UITextField *textField_temp2 = (UITextField *)[self.view viewWithTag:12];
    
    [textField_temp2 resignFirstResponder];
    
    UITextField *textField_temp3 = (UITextField *)[self.view viewWithTag:13];
    
    [textField_temp3 resignFirstResponder];
}

- (void) touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

//确定 执行事件
- (void) doSureAction
{
    [self commentTableViewTouchInSide];
    
    //传递数据
    if ([self.vehicleDelegate respondsToSelector:@selector(pathSelfPurchaseVehicleData:)]) {
        [self.vehicleDelegate pathSelfPurchaseVehicleData:self.mutableArray_inputContent];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
